package features.advance.leetcode.string.easy;

/**
 * @author LIN
 * @date 2022-04-30 23:03
 */
public class Solution58 {

    public static void main(String[] args) {
        Solution solution = new Solution() {
            @Override
            public int lengthOfLastWord(String s) {
                int index = s.length() - 1;
                while (s.charAt(index) == ' ') {
                    index--;
                }
                int wordLength = 0;
                while (index >= 0 && s.charAt(index) != ' ') {
                    wordLength++;
                    index--;
                }
                return wordLength;
            }
        };
        String s = "Hello World";
        int length = solution.lengthOfLastWord(s);
        System.out.println(length);
    }

    static class Solution {
        public int lengthOfLastWord(String s) {
            int len = 0;
            if(s == null || s.isEmpty()){
                return len;
            }
            for (int i = s.toCharArray().length - 1; i >= 0; i--) {
                char c = s.toCharArray()[i];
                if(!Character.isLetter(c) && c != ' '){
                    return 0;
                }
                if(c == ' ' && len==0){
                    continue;
                }else if(c != ' '){
                    len++;
                }else{
                    break;
                }
            }
            return len;
        }
    }
}
